﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.DAL;
using MINERP.MODEL.FoundationModel;
using System.Transactions;
using MINERP.MODEL.MobileMsgSend;
using MINERP.COM;

namespace MINERP.BLL.MobileMsgSend
{
    public class MobileMsgSendBLL
    {
        private newSampleYPDBEntities dataBase = new newSampleYPDBEntities();
        public List<Shop_Info> GetShop()
        {
            List<Shop> listShop = dataBase.Shop.ToList();
            List<Shop_Info> rtnShop = new List<Shop_Info>();
            foreach (Shop shop in listShop)
            {
                Shop_Info info = new Shop_Info() { ShopId = shop.ShopId, ShopName = shop.shopName, Address = shop.address, Area = shop.Area, ShopKeeper = shop.shopKeeper, Salesman = shop.Salesman, Tel2 = shop.Tel2 };
                rtnShop.Add(info);
            }
            return rtnShop;
        }
        /// <summary>
        /// 添加要发送短息
        /// </summary>
        /// <param name="inTellist">发送列表</param>
        /// <param name="inContent">发送内容</param>
        /// <param name="inPriority">加急级别</param>
        /// <param name="inBookDateTime">预定发送时间</param>
        /// <param name="inType">是否回复  0否1是</param>
        /// <param name="inReceiveId">回复短息的ID</param>
        /// <param name="inCreateUserId">创建人ID</param>
        public bool SendToDB(List<TelList> inTellist, string inContent, int iType, DateTime inBookDateTime, int inType, int inReceiveId, int inCreateUserId)
        {

            TransactionOptions options = new TransactionOptions();
            options.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
            using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required, options))
            {
                foreach (TelList tel in inTellist)
                {
                    SMSend send = new SMSend();
                    send.BookDateTime = inBookDateTime;
                    send.CreateTime = DateTime.Now;
                    send.CreateUser = inCreateUserId;
                    send.FailCount = 0;
                    send.Priority = 0;
                    send.Type = inType;
                    send.ReceiveId = inReceiveId;
                    send.SendTo = tel.Tel;
                    send.SendToShopName = tel.SendTo;
                    send.SendContent = inContent;
                    send.status = (int)SMSendStatus.waitSend;
                    dataBase.AddToSMSend(send);
                    dataBase.SaveChanges();
                }

                ts.Complete();
                return true;
            }

        }
        /// <summary>
        /// 获取未发送的列表
        /// </summary>
        /// <returns></returns>
        public List<SMSend_Info> GetUnSendMessage()
        {
            List<SMSend_Info> rtnMessageList = new List<SMSend_Info>();
            List<SMSend> SendList = dataBase.SMSend.ToList().OrderByDescending(p => p.Priority).OrderBy(p => p.CreateTime).ToList();
            foreach (SMSend send in SendList)
            {
                //定义员工
                users user = dataBase.users.FirstOrDefault(p => p.userID == send.CreateUser);
                string UserName = "";
                if (user != null)
                {
                    UserName = user.UserName;
                }
                //定义状态
                string status = "";
                switch (send.status)
                {
                    case (int)SMSendStatus.waitSend:
                        status = "等待发送";
                        break;
                    case (int)SMSendStatus.Error:
                        status = "错误";
                        break;
                    case (int)SMSendStatus.Sended:
                        status = "已发送";
                        break;
                    default:
                        status = "未知";
                        break;

                }
                SMSend_Info info = new SMSend_Info() { BookDateTime = send.BookDateTime, CreateTime = send.CreateTime, CreateUser = UserName, FailCount = send.FailCount, Priority = send.Priority, ReceiveId = send.ReceiveId, SendContent = send.SendContent, SendId = send.SendId, SendTo = send.SendTo, SendToShopName = send.SendToShopName, Status = status, Type = send.Type };
                rtnMessageList.Add(info);
            }
            return rtnMessageList;
        }
        /// <summary>
        /// 获取已经发送的记录分页显示
        /// </summary>
        /// <param name="inCurrPage">当前页码</param>
        /// <param name="inPageNum">每页记录数</param>
        /// <param name="outCount">总记录数</param>
        /// <returns></returns>
        public List<SMSended_Info> GetSendedMessage(int inCurrPage, int inPageNum, out int outCount)
        {
            List<SMSended_Info> rtnList = new List<SMSended_Info>();
            outCount = dataBase.SMSended.Count();
            List<SMSended> sendedList = dataBase.SMSended.OrderByDescending(p => p.SendTime).Skip((inCurrPage - 1) * inPageNum).Take(inPageNum).ToList();
            foreach (SMSended r in sendedList)
            {
                //定义员工
                users user = dataBase.users.FirstOrDefault(p => p.userID == r.CreateUser);
                string UserName = "";
                if (user != null)
                {
                    UserName = user.UserName;
                }
                SMSended_Info info = new SMSended_Info() { CreateTime = r.CreateTime, CreateUser = UserName, Priority = r.Priority, SendContent = r.SendContent, SendedId = r.SendedId, SendId = r.SendId, SendTime = r.SendTime, SendTo = r.SendTo, SendToShopName = r.SendToShopName, Status = "已发送" };
                rtnList.Add(info);
            }
            return rtnList;

        }
        /// <summary>
        /// 获取收件箱列表分页显示
        /// </summary>
        /// <param name="inCurrPage"></param>
        /// <param name="inPageNum"></param>
        /// <param name="outCount"></param>
        /// <returns></returns>
        public List<SMReceive_Info> GetReceiveMessage(int inCurrPage, int inPageNum, out int outCount)
        {
            List<SMReceive_Info> rtnList = new List<SMReceive_Info>();
            outCount = dataBase.SMReceive.Count();
            List<SMReceive> receiveList = dataBase.SMReceive.OrderByDescending(p => p.createTime).Skip((inCurrPage - 1) * inPageNum).Take(inPageNum).ToList();
            foreach (SMReceive r in receiveList)
            {
                string status = "";
                if (r.status == (int)SMReceiveStatus.waitReceive)
                {
                    status = "未回复";
                }
                else if (r.status == (int)SMReceiveStatus.Received)
                {
                    status = "已回复";
                }
                SMReceive_Info info = new SMReceive_Info() { CreateTime = r.createTime, ReceiveContent = r.ReceiveContent, ReceiveId = r.ReceiveId, ReceiveTime = r.ReceiveTime, SendFormShop = r.SendFormShop, SendFrom = r.SendFrom, Status = status };
                rtnList.Add(info);
            }
            return rtnList;
        }


        public void DeleteSMReceive(List<SMReceive_Info> inList)
        {
            foreach (SMReceive_Info info in inList)
            {
                SMReceive obj = dataBase.SMReceive.FirstOrDefault(p => p.ReceiveId == info.ReceiveId);
                dataBase.DeleteObject(obj);

            }
            dataBase.SaveChanges();
        }

        public void DeleteSMSended(List<SMSended_Info> inList)
        {
            foreach (SMSended_Info info in inList)
            {
                SMSended obj = dataBase.SMSended.FirstOrDefault(p => p.SendId == info.SendId);
                dataBase.DeleteObject(obj);
            }
            dataBase.SaveChanges();
        }
        public void DeleteSMSend(List<SMSend_Info> inList)
        {
            foreach(SMSend_Info info in inList)
            {
                SMSend obj=dataBase.SMSend.FirstOrDefault(p=>p.SendId==info.SendId);
                dataBase.DeleteObject(obj);
            }
            dataBase.SaveChanges();
        }
        /// <summary>
        /// 根据条件获取未发送信息
        /// </summary>
        /// <param name="inCondition"></param>
        /// <returns></returns>
        public List<SMSend_Info> GetSendBycondition(string inCondition)
        {
            List<SMSend_Info> rtnList = new List<SMSend_Info>();
            List<SMSend> sendList = dataBase.CreateQuery<SMSend>("select value it from  SMSend as it where " + inCondition).ToList();
            foreach (SMSend send in sendList)
            {
                //定义员工
                users user = dataBase.users.FirstOrDefault(p => p.userID == send.CreateUser);
                string UserName = "";
                if (user != null)
                {
                    UserName = user.UserName;
                }
                //定义状态
                string status = "";
                switch (send.status)
                {
                    case (int)SMSendStatus.waitSend:
                        status = "等待发送";
                        break;
                    case (int)SMSendStatus.Error:
                        status = "错误";
                        break;
                    case (int)SMSendStatus.Sended:
                        status = "已发送";
                        break;
                    default:
                        status = "未知";
                        break;

                }
                SMSend_Info info = new SMSend_Info() { BookDateTime = send.BookDateTime, CreateTime = send.CreateTime, CreateUser = UserName, FailCount = send.FailCount, Priority = send.Priority, ReceiveId = send.ReceiveId, SendContent = send.SendContent, SendId = send.SendId, SendTo = send.SendTo, SendToShopName = send.SendToShopName, Status = status, Type = send.Type };
                rtnList.Add(info);
            }
            return rtnList;
        }
        /// <summary>
        /// 根据条件获得已发送列表
        /// </summary>
        /// <param name="inCondition"></param>
        /// <returns></returns>
        public List<SMSended_Info> GetSendedByCondition(string inCondition)
        {
            List<SMSended_Info> rtnList = new List<SMSended_Info>();
            List<SMSended> sendedList = dataBase.CreateQuery<SMSended>("select value it from SMSended  as it where " + inCondition).ToList();
            foreach (SMSended r in sendedList)
            {
                //定义员工
                users user = dataBase.users.FirstOrDefault(p => p.userID == r.CreateUser);
                string UserName = "";
                if (user != null)
                {
                    UserName = user.UserName;
                }
                SMSended_Info info = new SMSended_Info() { CreateTime = r.CreateTime, CreateUser = UserName, Priority = r.Priority, SendContent = r.SendContent, SendedId = r.SendedId, SendId = r.SendId, SendTime = r.SendTime, SendTo = r.SendTo, SendToShopName = r.SendToShopName, Status = "已发送" };
                rtnList.Add(info);
            }
            return rtnList;
        }
        /// <summary>
        /// 根据条件获得收件信息
        /// </summary>
        /// <param name="inCondition"></param>
        /// <returns></returns>
        public List<SMReceive_Info> GetReceiveByCondition(string inCondition)
        {
            List<SMReceive_Info> rtnList = new List<SMReceive_Info>();
            List<SMReceive> receiveList = dataBase.CreateQuery<SMReceive>("select value it from SMReceive as it where " + inCondition).ToList();
            foreach (SMReceive r in receiveList)
            {
                string status = "";
                if (r.status == (int)SMReceiveStatus.waitReceive)
                {
                    status = "未回复";
                }
                else if (r.status == (int)SMReceiveStatus.Received)
                {
                    status = "已回复";
                }
                SMReceive_Info info = new SMReceive_Info() { CreateTime = r.createTime, ReceiveContent = r.ReceiveContent, ReceiveId = r.ReceiveId, ReceiveTime = r.ReceiveTime, SendFormShop = r.SendFormShop, SendFrom = r.SendFrom, Status = status };
                rtnList.Add(info);
            }
            return rtnList;

        }
    }
}
